package org.exez.modules.visitor.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.csb.sdk.HttpCaller;
import com.alibaba.csb.sdk.HttpCallerException;
import com.alibaba.csb.sdk.HttpParameters;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.exez.modules.visitor.dto.police.PoliceRootDTO;
import org.exez.modules.visitor.dto.zhongzhi.Data;
import org.exez.modules.visitor.dto.zhongzhi.ZhongZhiRootDTO;
import org.exez.modules.visitor.entity.KeyPersonnel;
import org.exez.modules.visitor.service.IKeyPersonnelService;
import org.exez.modules.visitor.service.PoliceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class PoliceServiceImpl implements PoliceService {

    @Autowired
    private IKeyPersonnelService keyPersonnelService;

    @Override
    public String getToken(String ip, String port) {
        return "";
    }

    private static final String success = "Success";

    @Override
    public Boolean getPoliceByIdentityValue(String identityValue) {
        String url = "http://csb-broker.yun.zj:8086/CSB";
        String appKey = "301ecd2e5b8a498aa7f314385090427b";// 众智平台中应用的App ID
        String secretKey = "dGm4CcAXoCAprGScqfIVoudGnWQ=";// 众智平台中应用的App Secret

        String requestId = "20200628172500041";

        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><DATAS><REQUESTID>" + requestId + "</REQUESTID><SFZH>" + identityValue + "</SFZH></DATAS>";
        HttpParameters.Builder hp = HttpParameters.newBuilder();
        hp.requestURL(url);
        hp.api("ST_JWZH_DQRYXX_001").version("1.0.0");
        hp.accessKey(appKey).secretKey(secretKey);
        hp.method("get");
        hp.putParamsMap("inxml", xml);

        PoliceRootDTO policeRootDTO = null;
        try {
            String invoke = HttpCaller.invoke(hp.build());

            policeRootDTO = JSONUtil.toBean(invoke, PoliceRootDTO.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        if (null != policeRootDTO && null != policeRootDTO.getDATAS()
                && null != policeRootDTO.getDATAS().getRETURNMSG()
                && StringUtils.isNotBlank(policeRootDTO.getDATAS().getRETURNMSG().getUSERNUMBER())) {
            System.out.printf("1111111111111111111111111111");
            return true;
        } else {
            System.out.printf("2222222222222222222222222222");
            return false;
        }

    }

    @Override
    public Boolean saveKeyPersonnel() {
        try {

            List<String> list =
                    Arrays.asList("330000000",
                            "330100000",
                            "330200000",
                            "330300000",
                            "330400000",
                            "330500000",
                            "330600000",
                            "330700000",
                            "330800000",
                            "330900000",
                            "331000000",
                            "331100000");
//
//            Arrays.asList("331004530000");

            for (String s : list) {
                ZhongZhiRootDTO zhongZhiRootDTO = getResult(s);
                if (success.equals(zhongZhiRootDTO.getStatus())) {
                    continue;
                }
                for (int i = 0; i < 3; i++) {
                    zhongZhiRootDTO = getResult(s);
                    if (success.equals(zhongZhiRootDTO.getStatus())) {
                        break;
                    }
                }

            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    private ZhongZhiRootDTO getResult(String pcsdm) throws HttpCallerException {
        //TODO 地址 appKey  secrectKey requestId 未填写
        String url = "http://csb-broker.yun.zj:8086/CSB";
        String appKey = "301ecd2e5b8a498aa7f314385090427b";// 众智平台中应用的App ID
        String secretKey = "dGm4CcAXoCAprGScqfIVoudGnWQ=";// 众智平台中应用的App Secret

        HttpParameters.Builder hp = HttpParameters.newBuilder();
        hp.api("ZJ_YJZX_ZDRY").version("1.0.0");
        hp.requestURL(url);
        hp.accessKey(appKey).secretKey(secretKey);
        hp.method("post");
        hp.putParamsMap("pcsdm", pcsdm);
        hp.putParamsMap("start", "0");
        hp.putParamsMap("size", "");
        hp.putParamsMap("User_ID", "029447");
        //hp.putParamsMap("Organization", "单位名称");
        //hp.putParamsMap("Organization_ID", "单位机构代码");
        //hp.putParamsMap("User_Name", "用户名");
        //hp.putParamsMap("Terminal_ID", "42.204.2.65");
        String invoke = HttpCaller.invoke(hp.build());
        System.out.println("invoke:" + invoke);

        ZhongZhiRootDTO zhongZhiRootDTO = JSONUtil.toBean(invoke, ZhongZhiRootDTO.class);

        if (null != zhongZhiRootDTO && success.equals(zhongZhiRootDTO.getStatus())
                && CollectionUtils.isNotEmpty(zhongZhiRootDTO.getData())) {

            List<Data> data = zhongZhiRootDTO.getData();

            for (Data datum : data) {
                if (StringUtils.isBlank(datum.getGmsfhm())) {
                    continue;
                }
                KeyPersonnel keyPersonnel = new KeyPersonnel();
                BeanUtils.copyProperties(datum, keyPersonnel);

                keyPersonnel.setHjdPcs(datum.getHjd_pcs());
                keyPersonnel.setHjdPcsdm(datum.getHjd_pcsdm());
                keyPersonnel.setHjdzXxdz(datum.getHjdz_xxdz());
                keyPersonnel.setXzdPcs(datum.getXzd_pcs());
                keyPersonnel.setXzdPcsdm(datum.getXzd_pcsdm());
                keyPersonnel.setXzdXxdz(datum.getXzd_xxdz());

                KeyPersonnel byId = keyPersonnelService.getById(keyPersonnel.getGmsfhm());
                if (byId == null) {
                    keyPersonnelService.save(keyPersonnel);
                } else {
                    keyPersonnelService.updateById(keyPersonnel);
                }

            }
        }

        return zhongZhiRootDTO;
    }
}
